<template>
	<view>
		<view class="allMargin" style="margin-top: 50rpx;">
			<form @submit="formSubmit">
				<input name="password1" type="password" placeholder="请输入密码" class="passwordInput" />
				<input name="password2" type="password" placeholder="请再次确认密码" class="passwordInput" style="margin-top: 30rpx;" />
				<view class="passwordRule">
					<text>密码长度8-32位，须包含数字、字母、符号至少两种或以上元素。</text>
				</view>
				<!-- <input class=" invite-code-input" placeholder="输入邀请码" /> -->
				<button form-type="submit" class="common-button">确认提交</button>
			</form>
		</view>
	</view>
</template>

<script>
	import md5 from 'js-md5'
	import {
		userRegister
	} from '@/common/api/quickRegister/quickRegister.js'
	var graceChecker = require("@/common/graceChecker.js"); //graceUI的表单验证
	export default {
		props: ['phoneData'],
		data() {
			return {
				info: '',
				password1: '',
				password2: ''
			}
		},
		methods: {
			formSubmit: function(e) {
				this.info = e.target.value;
				this.password1 = this.info.password1.trim();
				this.password2 = this.info.password2.trim();
				var regPassword = /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{8,32}$/;
				var rule = [
					//验证密码格式：密码长度8-32位，须包含数字、字母、符号至少两种或以上元素。
					{
						name: "password1",
						checkType: "reg",
						checkRule: regPassword,
						errorMsg: "密码1格式错误！",
					},
					{
						name: "password2",
						checkType: "reg",
						checkRule: regPassword,
						errorMsg: "密码2格式错误！",
					},
					//验证两个密码是否相等==>same
					{
						name: "password1",
						checkType: "same",
						checkRule: this.password2,
						errorMsg: "您两次输入的密码不一致！",
					},
				];
				var checkRes = graceChecker.check(this.info, rule);
				if (checkRes) {
					let userInfo = uni.getStorageSync("userInfo")
					userRegister(this.phoneData.phoneNumber,this.phoneData.authCode,{
						avatarUrl: userInfo.avatarUrl,
						city: userInfo.city,
						gender: userInfo.gender,
						major: userInfo.major,
						nickName: userInfo.nickName,
						province: userInfo.province,
						password: md5(this.password1)
					}).then(res => {
						if(res.data.code == 200) {
							console.log("注册成功"+res)
							uni.showToast({
								title: '注册成功',
								icon: 'success'
							})
							uni.reLaunch({
								url: '/pages/login/phoneLogin/phoneLogin?phoneNumber=' + this.phoneData.phoneNumber
							})
						} else{
							uni.showToast({
								title: '网络错误',
								icon: 'none',
								mask: true,
								duration: 1000
							})
						}
					})
				} else {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
				}
			}
		},
		onLoad() {
			console.log("数据"+this.phoneData)
			console.log(this.phoneData.phoneNumber)
		}
	}
</script>

<style>
	page {
		background: #FFFFFF;
	}

	.passwordRule {
		padding: 0 20rpx;
		font-size: 28rpx;
		color: #A9A9A9;
		margin: 20rpx 0 50rpx;
	}

	.invite-code-input {
		width: 30%;
		background-color: #FFFFFF;
		padding: 15rpx 25rpx;
		text-align: left;
		font-size: 28rpx;
		line-height: 60rpx;
		border-radius: 8rpx;
	}
</style>
